package org.jetnuke.modules.submitnews;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Censore;
import org.jetnuke.entities.MainConfig;
import org.jetnuke.entities.Queue;
import org.jetnuke.servlets.utilities.StringUtils;
import org.jetnuke.util.JetUtil;

public class DoSubmit implements IAction {
	String story, story_ext;

	String submit;

	String posttype;

	public void setPosttype(String posttype) {
		this.posttype = posttype;
	}

	public void setSubmit(String submit) {
		this.submit = submit;
	}

	int category;

	String title;

	public void setCategory(int category) {
		this.category = category;
	}

	public void setStory(String story) {
		this.story = story;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, javax.sql.DataSource,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (submit != null) {
			MainConfig config = (MainConfig) request
					.getAttribute("MAIN_CONFIG");
			int censorMode = config.getCENSORMODE();
			String censorReplace = config.getCENSORREPLACE();
			Queue queue = new Queue();
			List censorList = new ArrayList();
			Iterator it = DataAccessDriver.getInst().newCensoreDAO().selectAll().iterator();// censorePeer.doSelect(new Criteria()).iterator();
			while (it.hasNext()) {
				censorList.add(((Censore) it.next()).getWORD());
			}

			queue.setSTORYEXT(StringUtils.filterText(censorMode, censorReplace,
					story_ext, censorList,false));
			queue.setSTORY(StringUtils.filterText(censorMode, censorReplace,
					story, censorList,false));
			if (ui != null) {
				queue.setUID(ui.getID());
				queue.setUNAME(ui.getUname());
			}
			queue.setTOPIC(category);
			queue.setSUBJECT(StringUtils.filterText(censorMode, censorReplace,
					title, censorList,true));
			DataAccessDriver.getInst().newQueueDAO().addNew(queue);
			//queue.save();
			JetUtil.redirect(vc,response,request.getContextPath()+"/do/submit_news/received");
			return null;
		} else {
			String f_story = null;//$story;
			String f_storyext = null;//$f_storyext = $storyext;
			//if (posttype.equals("plaintext")) {
			//	f_story = story.replaceAll("\n", "<br/>"); //equivalent of PHP
														   // function "nl2br"
			//	f_storyext = story_ext.replaceAll("\n", "<br/>");
			//} else {
				f_story = story;
				f_storyext = story_ext;
			//}
			String story2 = f_story + "<br/><br/>" + f_storyext;
			vc.put("story2", story2);
			vc.put("cats", DataAccessDriver.getInst().newTopicsDAO().selectAll());// topicsPeer.doSelect(new Criteria()));
			vc.put("sel_cat",  new Integer(category));
			return new ActionForward("preview_page", info);
		}
	}

	/**
	 * @param story_ext
	 *            The story_ext to set.
	 */
	public void setStory_ext(String story_ext) {
		this.story_ext = story_ext;
	}

}
